package org.coderjonny.util;

import com.alibaba.nacos.common.utils.RandomUtils;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

import java.util.List;

public class DiscoveryUtil {
    public static final String PRODUCT_SERVICE = "server-product";
    public static final String USER_SERVICE = "server-user";

    public static String getServiceUrl(String service, DiscoveryClient discoveryClient){
        List<ServiceInstance> instanceList = discoveryClient.getInstances(service);
        int index = RandomUtils.nextInt(0, instanceList.size());
        ServiceInstance instance = instanceList.get(index);
        return instance.getHost() + ":" + instance.getPort();
    }
}
